package org.zhengquan.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zhengquan.common.JsonData;
import org.zhengquan.dto.DeptLevelDto;
import org.zhengquan.service.SysDeptService;
import org.zhengquan.service.SysTreeService;
import org.zhengquan.vo.DeptVO;

import java.util.List;

/**
 * @author zhengquan
 * @date 2017/10/27
 */
@Controller
@RequestMapping("/sys/dept/")
@Slf4j
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysTreeService sysTreeService;

    @RequestMapping("dept.page")
    public ModelAndView page() {
        return new ModelAndView("dept");
    }


    @RequestMapping("save.json")
    @ResponseBody
    public JsonData save(DeptVO deptVO) {
        sysDeptService.save(deptVO);
        return JsonData.success();
    }

    @RequestMapping("tree.json")
    @ResponseBody
    public JsonData tree() {
        List<DeptLevelDto> deptLevelDtoList = sysTreeService.deptTree();
        return JsonData.success(deptLevelDtoList);
    }

    @RequestMapping("update.json")
    @ResponseBody
    public JsonData update(DeptVO deptVO) {
        sysDeptService.update(deptVO);
        return JsonData.success();
    }

    @RequestMapping("delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("id") Integer id){
        sysDeptService.delete(id);
        return JsonData.success();
    }

}
